package com.qiankun.tang.ui.view.adapter.find;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.common.library.listener.OnAppListener;
import com.common.library.net.items.BaseItem;
import com.common.library.ui.view.LinearSpacingItemDecoration;
import com.common.library.ui.view.adapter.base.BaseRecyclerAdapter;
import com.qiankun.tang.R;
import com.qiankun.tang.beans.ADBean;
import com.qiankun.tang.beans.VideoSynopsisBean;
import com.qiankun.tang.beans.items.ClassifyItem;
import com.qiankun.tang.ui.view.HorizontalListWidget;
import com.qiankun.tang.ui.view.VideoSynopsisListWidget;
import com.qiankun.tang.ui.view.adapter.holder.BaseLineHolder;
import com.qiankun.tang.ui.view.adapter.holder.BaseTitleHolder;

import java.util.List;

/**
 * @author txf
 * @Title
 * @package com.qiankun.video.ui.view.adapter
 * @date 2017/6/9 0009
 */

public class ClassifyAdapter extends BaseRecyclerAdapter<ClassifyItem> {
    public ClassifyAdapter(Context context) {
        super(context);
    }
    public ClassifyAdapter(Context context, OnAppListener.OnInteractionListener listener) {
        super(context, listener);
    }
    @Override
    public int getItemViewType(int position) {
        return getDatas().get(position).getType();
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case ClassifyItem.TYPE_0:
                return new ClassifyViewHolder0(LayoutInflater.from(getContext()).inflate(R.layout.item_classify_0, parent, false));
            case ClassifyItem.TYPE_1:
                return new ClassifyViewHolder1(LayoutInflater.from(getContext()).inflate(R.layout.item_classify_1, parent, false));
            case BaseItem.TYPE_TITLE:
                return new BaseTitleHolder(LayoutInflater.from(getContext()).inflate(R.layout.item_base_title, parent, false));
            case BaseItem.TYPE_LINE:
                return new BaseLineHolder(LayoutInflater.from(getContext()).inflate(R.layout.item_base_line, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ClassifyViewHolder0) {
            onClassifyViewHolder0((ClassifyViewHolder0) holder, position);
        }else if(holder instanceof ClassifyViewHolder1){
            onClassifyViewHolder1((ClassifyViewHolder1) holder, position);
        }else if(holder instanceof BaseTitleHolder){
            onBindBaseTitleHolder((BaseTitleHolder) holder, position);
        }
    }
    private void onClassifyViewHolder1(ClassifyViewHolder1 holder, int position) {
        final List<VideoSynopsisBean> beans = (List<VideoSynopsisBean>) getDatas().get(position).getBean();
        holder.mVideoSynopsisLisetWidget.setData(beans);
        holder.mVideoSynopsisLisetWidget.addOnItemClickListener(new VideoSynopsisListWidget.OnItemClickListener() {
            @Override
            public void onItemClick(int type, VideoSynopsisBean bean) {
                getListener().onInteraction(ClassifyItem.TYPE_1,bean);
            }
        });
    }
    private void onBindBaseTitleHolder(BaseTitleHolder holder, int position) {
        String title = (String) getDatas().get(position).getBean();
        holder.mTitle.setText(title);
    }
    private void onClassifyViewHolder0(ClassifyViewHolder0 holder, int position) {
        List<ADBean> datas = (List<ADBean>) getDatas().get(position).getBean();
        holder.mHorizontalListWidget.setData(datas,0);
        holder.mHorizontalListWidget.addOnClickListaner(new HorizontalListWidget.OnClickListaner() {
            @Override
            public void onItemClick(ADBean bean) {
                getListener().onInteraction(ClassifyItem.TYPE_0,bean);
            }
        });
    }
    class ClassifyViewHolder0 extends RecyclerView.ViewHolder {
        HorizontalListWidget mHorizontalListWidget;
        public ClassifyViewHolder0(View itemView) {
            super(itemView);
            mHorizontalListWidget = (HorizontalListWidget) itemView.findViewById(R.id.item_classify_HorizontalListWidget);
        }
    }
    class ClassifyViewHolder1 extends RecyclerView.ViewHolder {
        VideoSynopsisListWidget mVideoSynopsisLisetWidget;
        public ClassifyViewHolder1(View itemView) {
            super(itemView);
            mVideoSynopsisLisetWidget = (VideoSynopsisListWidget) itemView.findViewById(R.id.item_classify_1_VideoSynopsisLisetWidget);
            mVideoSynopsisLisetWidget.setItemDecoration(new LinearSpacingItemDecoration(LinearLayoutManager.HORIZONTAL,dip2px(4),true));
        }
    }
}
